package com.warmheart.core.rs.builder;

import java.util.Comparator;
import java.util.Iterator;

import javax.validation.constraints.NotNull;

import com.warmheart.core.cors.builder.CoRsBuilder;
import com.warmheart.core.rs.enums.FieldTypeEnum;
import com.warmheart.core.rs.enums.SmTypeEnum;
import com.warmheart.core.rs.model.SelectedModel;

class WarpperBuilder {

    static <T extends CoRsBuilder<?, ?>> WarpperBuilder.Builder<T> custom(@NotNull T entity) {
        return new Builder<T>(entity);
    }

    static class Builder<T extends CoRsBuilder<?, ?>> {

        private T entity;

        protected Builder(T entity) {
            super();
            this.entity = entity;
        }

        public void single(Boolean... distinctFlags) {
            if (this.entity.getSelectedModelList().size() > 0) {
                Iterator<SelectedModel> iterator = this.entity.getSelectedModelList().iterator();
                while (iterator.hasNext()) {
                    SelectedModel element = iterator.next();
                    if (!element.getFieldType().equals(FieldTypeEnum.SINGLE.getValue())) {
                        iterator.remove();
                    }
                }
            }
            if (this.entity.getSelectedModelList().size() == 0) {
                // 如果列表为空，默认获取ID相关信息
                this.entity.getSelectedModelList().add(new SelectedModel(FieldTypeEnum.SINGLE, "id", "id"));
            }
            if (distinctFlags != null && distinctFlags.length > 0) {
                this.entity.getDistinctModel().setDistinctFlag(distinctFlags[0]);
            }
            this.entity.getDistinctModel().setSmType(SmTypeEnum.SINGLE.getValue());
        }

        public void multiple(Boolean... distinctFlags) {
            if (this.entity.getSelectedModelList().size() > 0) {
                Iterator<SelectedModel> iterator = this.entity.getSelectedModelList().iterator();
                while (iterator.hasNext()) {
                    SelectedModel element = iterator.next();
                    if (!element.getFieldType().equals(FieldTypeEnum.MULTIPLE_FIRST.getValue())
                            && !element.getFieldType().equals(FieldTypeEnum.MULTIPLE_NEXT.getValue())) {
                        iterator.remove();
                    }
                }
            }
            if (this.entity.getSelectedModelList().size() == 0) {
                // 如果列表为空，默认获取ID相关信息
                this.entity.getSelectedModelList().add(new SelectedModel(FieldTypeEnum.MULTIPLE_FIRST, "id", "id"));
            }
            // 排序（首字段排第一位置）
            this.entity.getSelectedModelList().sort(new Comparator<SelectedModel>() {

                @Override
                public int compare(SelectedModel o1, SelectedModel o2) {
                    if (o1.getFieldType().equals(FieldTypeEnum.MULTIPLE_FIRST.getValue())) {
                        return -1;
                    }
                    if (o2.getFieldType().equals(FieldTypeEnum.MULTIPLE_FIRST.getValue())) {
                        return 1;
                    }
                    return 0;
                }
            });
            if (distinctFlags != null && distinctFlags.length > 0) {
                this.entity.getDistinctModel().setDistinctFlag(distinctFlags[0]);
            }
            this.entity.getDistinctModel().setSmType(SmTypeEnum.MULTIPLE.getValue());
        }

        public void groupByCnt(Boolean... distinctFlags) {
            if (this.entity.getSelectedModelList().size() > 0) {
                Iterator<SelectedModel> iterator = this.entity.getSelectedModelList().iterator();
                while (iterator.hasNext()) {
                    SelectedModel element = iterator.next();
                    if (!element.getFieldType().equals(FieldTypeEnum.GROUP.getValue())
                            && !element.getFieldType().equals(FieldTypeEnum.COUNT.getValue())) {
                        iterator.remove();
                    }
                }
            }
            if (this.entity.getSelectedModelList().size() == 0) {
                // 如果列表为空，默认获取ID相关信息
                this.entity.getSelectedModelList().add(new SelectedModel(FieldTypeEnum.GROUP, "id", "id"));
            }
            // 排序（分组字段排第一位置）
            this.entity.getSelectedModelList().sort(new Comparator<SelectedModel>() {

                @Override
                public int compare(SelectedModel o1, SelectedModel o2) {
                    if (o1.getFieldType().equals(FieldTypeEnum.GROUP.getValue())) {
                        return -1;
                    }
                    if (o2.getFieldType().equals(FieldTypeEnum.GROUP.getValue())) {
                        return 1;
                    }
                    return 0;
                }
            });
            if (distinctFlags != null && distinctFlags.length > 0) {
                this.entity.getDistinctModel().setDistinctFlag(distinctFlags[0]);
            }
            this.entity.getDistinctModel().setSmType(SmTypeEnum.GROUP_BY_CNT.getValue());
        }

        public void keyValue(Boolean... distinctFlags) {
            if (this.entity.getSelectedModelList().size() > 0) {
                Iterator<SelectedModel> iterator = this.entity.getSelectedModelList().iterator();
                while (iterator.hasNext()) {
                    SelectedModel element = iterator.next();
                    if (!element.getFieldType().equals(FieldTypeEnum.KEY.getValue())
                            && !element.getFieldType().equals(FieldTypeEnum.VALUE.getValue())) {
                        iterator.remove();
                    }
                }
            }
            if (this.entity.getSelectedModelList().size() == 0) {
                // 如果列表为空，默认获取ID相关信息
                this.entity.getSelectedModelList().add(new SelectedModel(FieldTypeEnum.KEY, "id", "key"));
            }
            // 排序（分组字段排第一位置）
            this.entity.getSelectedModelList().sort(new Comparator<SelectedModel>() {

                @Override
                public int compare(SelectedModel o1, SelectedModel o2) {
                    if (o1.getFieldType().equals(FieldTypeEnum.KEY.getValue())) {
                        return -1;
                    }
                    if (o2.getFieldType().equals(FieldTypeEnum.KEY.getValue())) {
                        return 1;
                    }
                    return 0;
                }
            });
            if (distinctFlags != null && distinctFlags.length > 0) {
                this.entity.getDistinctModel().setDistinctFlag(distinctFlags[0]);
            }
            this.entity.getDistinctModel().setSmType(SmTypeEnum.KEY_VALUE.getValue());
        }
    }
}
